﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Uniforax.Core.Common;
using Uniforax.Core.GameObjects;

namespace Uniforax.Core.GameActions.Battle
{
    public class DamageAction : BaseGameAction
    {
        public int Damage { get; set; }

        public PosId PosId { get; set; }
        public string ObjectId { get; set; }

        public int HP { get; set; }

        public DamageAction(BaseGameObject gameObject)
        {
            PosId = gameObject.PosId;
            ObjectId = gameObject.ObjectId;
            HP = gameObject.HP;
        }

        public override void Execute(ActionContext context)
        {
            var world = context.GameWorld;
            var grid = world.GetGrid(PosId);
            var gameObject = grid.GetPlacedObject(ObjectId);

            var finalHP = gameObject.HP - Damage;
            if (finalHP <= 0)
            {
                gameObject.HP = 0;
                var destoryAction = new DestoryGameObjectAction(gameObject);
                context.History.Add(destoryAction);
                destoryAction.Execute(context);
            }
            else if (Damage < 0)
            {
                gameObject.HP = finalHP;
                gameObject.OnHealed(context);
            }
            else
            {
                gameObject.HP = finalHP;
            }
        }


        public override void Undo(ActionContext context)
        {
            var world = context.GameWorld;
            var grid = world.GetGrid(PosId);
            var gameObject = grid.GetPlacedObject(ObjectId);

            gameObject.HP = HP;

        }

    }
}
